package com.rrd.hive.udf;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.hadoop.hive.ql.exec.UDF;


public class ParseTableUDF extends UDF{
	Pattern pattern = Pattern.compile(" |;|\t|--|\n|\r|\\\\n|\\\\r");
	Pattern p = Pattern.compile("(^(fdm\\.|tmp\\.|adm\\.|bdm\\.|gdm\\.|[^\\s]+tmp\\.|mongo_|[a-z]{3,}\\d{2}_).*)|.*(_chain|_inc|_all)$");
	Matcher matcher =null;
	public  List<String> evaluate(String sql) {
		List<String> list = new ArrayList<String>();
    	String arr[] = null;
    	if(sql==null){
    		return null;
    	}
		sql = sql.replace("`", "");
    	arr =pattern.split(sql);
    	 
    	for(int i=0;i<arr.length;i++){
    		 matcher=p.matcher(arr[i]);
    		 if(matcher.matches()){
    			 
    			list.add(arr[i]);
    		 }
    	}
    	return list;
	}
	
	public static void main(String[] args) {
		String sql="SELECT `mr_business_h5_regincome_d`.`cli_mhuanbi` AS `cli_mhuanbi`,   `mr_business_h5_regincome_d`.`cli_user_cnt_mtd` AS `cli_user_cnt_mtd`,   `mr_business_h5_regincome_d`.`cli_user_cnt_pmtd` AS `cli_user_cnt_pmtd`,   `mr_business_h5_regincome_d`.`cli_uv_rate_mhuanbi` AS `cli_uv_rate_mhuanbi`,   `mr_business_h5_regincome_d`.`cli_uv_rate_mtd` AS `cli_uv_rate_mtd`,   `mr_business_h5_regincome_d`.`cli_uv_rate_pmtd` AS `cli_uv_rate_pmtd`,   CAST (TO_DATE(`mr_business_h5_regincome_d`.`dt`) AS DATE) AS `dt`,   `mr_business_h5_regincome_d`.`income_huanbi` AS `income_huanbi`,   `mr_business_h5_regincome_d`.`income_mtd` AS `income_mtd`,   `mr_business_h5_regincome_d`.`income_pmtd` AS `income_pmtd`,   `mr_business_h5_regincome_d`.`income_uv_rate_mhuanbi` AS `income_uv_rate_mhuanbi`,   `mr_business_h5_regincome_d`.`income_uv_rate_mtd` AS `income_uv_rate_mtd`,   `mr_business_h5_regincome_d`.`income_uv_rate_pmtd` AS `income_uv_rate_pmtd`,   `mr_business_h5_regincome_d`.`product_id` AS `product_id`,   `mr_business_h5_regincome_d`.`product_name` AS `product_name`,   `mr_business_h5_regincome_d`.`register_cli_mhuanbi` AS `register_cli_mhuanbi`,   `mr_business_h5_regincome_d`.`register_cli_mtd` AS `register_cli_mtd`,   `mr_business_h5_regincome_d`.`register_cli_pmtd` AS `register_cli_pmtd`,   `mr_business_h5_regincome_d`.`register_mhuanbi` AS `register_mhuanbi`,   `mr_business_h5_regincome_d`.`register_mtd` AS `register_mtd`,   `mr_business_h5_regincome_d`.`register_pmtd` AS `register_pmtd`,   `mr_business_h5_regincome_d`.`unit_price` AS `unit_price` FROM `adm`.`mr_business_h5_regincome_d` `mr_business_h5_regincome_d`";
		ParseTableUDF obj = new ParseTableUDF();
		List<String> arr = obj.evaluate(sql);
		for(String s:arr){
			System.out.println(s);
		}
	}
}
